package com.javacv.plus.examples.controller;

import com.javacv.plus.examples.service.ImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "图像处理", description = "图像处理相关接口")
@RestController
@RequestMapping("/images")
@RequiredArgsConstructor
public class ImageController {

    private final ImageService imageService;

    @Operation(summary = "调整图像大小")
    @PostMapping(value = "/resize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String resizeImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "宽度") @RequestParam("width") int width,
        @Parameter(description = "高度") @RequestParam("height") int height
    ) {
        return imageService.resizeImage(file, width, height);
    }

    @Operation(summary = "旋转图像")
    @PostMapping(value = "/rotate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String rotateImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "旋转角度") @RequestParam("angle") int angle
    ) {
        return imageService.rotateImage(file, angle);
    }

    @Operation(summary = "转换图像格式")
    @PostMapping(value = "/convert", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String convertImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "目标格式") @RequestParam("format") String format
    ) {
        return imageService.convertImage(file, format);
    }

    @Operation(summary = "压缩图像")
    @PostMapping(value = "/compress", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String compressImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "压缩质量") @RequestParam("quality") double quality
    ) {
        return imageService.compressImage(file, quality);
    }

    @Operation(summary = "添加水印")
    @PostMapping(value = "/watermark", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String addWatermark(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "水印图像") @RequestParam("watermark") MultipartFile watermark,
        @Parameter(description = "水印位置") @RequestParam("position") String position
    ) {
        return imageService.addWatermark(file, watermark, position);
    }

    @Operation(summary = "模糊图像")
    @PostMapping(value = "/blur", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String blurImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "模糊半径") @RequestParam("radius") int radius
    ) {
        return imageService.blurImage(file, radius);
    }

    @Operation(summary = "锐化图像")
    @PostMapping(value = "/sharpen", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String sharpenImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "锐化程度") @RequestParam("amount") double amount
    ) {
        return imageService.sharpenImage(file, amount);
    }

    @Operation(summary = "转换为灰度图")
    @PostMapping(value = "/grayscale", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String convertToGrayscale(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file
    ) {
        return imageService.convertToGrayscale(file);
    }

    @Operation(summary = "裁剪图像")
    @PostMapping(value = "/crop", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String cropImage(
        @Parameter(description = "图像文件") @RequestParam("file") MultipartFile file,
        @Parameter(description = "起始X坐标") @RequestParam("x") int x,
        @Parameter(description = "起始Y坐标") @RequestParam("y") int y,
        @Parameter(description = "宽度") @RequestParam("width") int width,
        @Parameter(description = "高度") @RequestParam("height") int height
    ) {
        return imageService.cropImage(file, x, y, width, height);
    }
} 